﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SST.Phone.Model.DAO;
using SST.Phone.Model.VO;
using SST.Phone.Util;

namespace SST.Phone.ViewModels
{
    public class SettingViewModel : INotifyPropertyChanged
    {
        #region Properties
        private string _LoginNickname;
        /// <summary>
        /// Login nickname
        /// </summary>
        public string LoginNickname
        {
            get { return _LoginNickname; }
            set
            {
                _LoginNickname = value;
                NotifyPropertyChanged("LoginNickname");
            }
        }

        private string _LoginPassword;
        /// <summary>
        /// Login password
        /// </summary>
        public string LoginPassword
        {
            get { return _LoginPassword; }
            set
            {
                _LoginPassword = value;
                NotifyPropertyChanged("LoginPassword");
            }
        }

        private int _ChatRefreshSeconds;
        /// <summary>
        /// Number of seconds between chat refresh
        /// </summary>
        public int ChatRefreshSeconds
        {
            get { return _ChatRefreshSeconds; }
            set
            {
                _ChatRefreshSeconds = value;
                NotifyPropertyChanged("ChatRefreshSeconds");
            }
        }

        private bool _ShowTray;
        /// <summary>
        /// Show or hide system tray
        /// </summary>
        public bool ShowTray
        {
            get { return _ShowTray; }
            set
            {
                _ShowTray = value;
                NotifyPropertyChanged("ShowTray");
            }
        }

        #endregion

        /// <summary>
        /// Load all settings to properties
        /// </summary>
        public void Load()
        {
            SettingsDAO settingDAO = new SettingsDAO(App.DB);

            LoginNickname = settingDAO.LoadValue(SettingItem.SETTING.USERNAME);
            LoginPassword = settingDAO.LoadValue(SettingItem.SETTING.PASSWORD);
            string chatRefresh = settingDAO.LoadValue(SettingItem.SETTING.CHAT_REFRESH);
            try
            {
                if (chatRefresh != null)
                    ChatRefreshSeconds = int.Parse(chatRefresh);
                else
                    ChatRefreshSeconds = 0;
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
                ChatRefreshSeconds = 0;
            }
            string _showTray = settingDAO.LoadValue(SettingItem.SETTING.SHOW_TRAY);
            if (_showTray == "true")
                ShowTray = true;
            else
                ShowTray = false;
        }

        /// <summary>
        /// Save all settings to database
        /// </summary>
        public void Save()
        {
            SettingsDAO settingDAO = new SettingsDAO(App.DB);

            settingDAO.Save(new SettingItem() { Name = SettingItem.SETTING.USERNAME.ToString(), Value = LoginNickname });
            settingDAO.Save(new SettingItem() { Name = SettingItem.SETTING.PASSWORD.ToString(), Value = LoginPassword });
            settingDAO.Save(new SettingItem() { Name = SettingItem.SETTING.CHAT_REFRESH.ToString(), Value = ChatRefreshSeconds.ToString() });
            settingDAO.Save(new SettingItem() { Name = SettingItem.SETTING.SHOW_TRAY.ToString(), Value = ShowTray.ToString().ToLower() });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
